package com.cvr.project.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.cvr.project.view.WatermarkView;

/**
 * 水印管理器
 * 提供全局的水印显示控制功能
 */
public class WatermarkManager {
    
    private static final String PREF_NAME = "watermark_settings";
    private static final String KEY_WATERMARK_ENABLED = "watermark_enabled";
    
    private static WatermarkManager instance;
    private boolean isWatermarkEnabled = false; // 默认开启
    
    private WatermarkManager() {
    }
    
    public static WatermarkManager getInstance() {
        if (instance == null) {
            synchronized (WatermarkManager.class) {
                if (instance == null) {
                    instance = new WatermarkManager();
                }
            }
        }
        return instance;
    }
    
    /**
     * 初始化水印管理器
     */
    public void init(Context context) {
//        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
//        isWatermarkEnabled = prefs.getBoolean(KEY_WATERMARK_ENABLED, true);
    }
    
    /**
     * 设置水印是否启用
     */
    public void setWatermarkEnabled(Context context, boolean enabled) {
        this.isWatermarkEnabled = enabled;
        
        // 保存到SharedPreferences
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putBoolean(KEY_WATERMARK_ENABLED, enabled).apply();
    }
    
    /**
     * 获取水印是否启用
     */
    public boolean isWatermarkEnabled() {
        return isWatermarkEnabled;
    }
    
    /**
     * 为Activity添加水印
     */
    public void addWatermark(Activity activity) {
        if (!isWatermarkEnabled) {
            return;
        }
        
        try {
            // 获取Activity的根布局
            ViewGroup rootView = (ViewGroup) activity.findViewById(android.R.id.content);
            if (rootView == null) {
                return;
            }
            
            // 检查是否已经添加了水印
            if (rootView.findViewWithTag("watermark_view") != null) {
                return;
            }
            
            // 创建水印View
            WatermarkView watermarkView = new WatermarkView(activity);
            watermarkView.setTag("watermark_view");
            
            // 创建布局参数，让水印覆盖整个屏幕
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT
            );
            
            // 添加水印到根布局
            rootView.addView(watermarkView, params);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 从Activity移除水印
     */
    public void removeWatermark(Activity activity) {
        if (!isWatermarkEnabled){
            return;
        }
        try {
            ViewGroup rootView = (ViewGroup) activity.findViewById(android.R.id.content);
            if (rootView == null) {
                return;
            }
            
            // 查找并移除水印View
            WatermarkView watermarkView = (WatermarkView) rootView.findViewWithTag("watermark_view");
            if (watermarkView != null) {
                rootView.removeView(watermarkView);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 刷新Activity的水印状态
     */
    public void refreshWatermark(Activity activity) {
        removeWatermark(activity);
        if (isWatermarkEnabled) {
            addWatermark(activity);
        }
    }
    
    /**
     * 切换水印显示状态
     */
    public void toggleWatermark(Context context) {
        setWatermarkEnabled(context, !isWatermarkEnabled);
    }
}